﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace WiMo.Games.Premium.GDIDisplay.ImageAPI
{
    static partial class GraphicsExtension
    {
        public static System.Drawing.Color GetTransparentColor(Image image)
        {
            return ((Bitmap)image).GetPixel(0, 0);
        }

        public static void DrawAlpha(this Graphics gx, Bitmap image, byte transparency, Location point)
        {
            DrawAlpha(gx, image, transparency, point.X, point.Y);
        }

        public static void DrawAlpha(this Graphics gx, Bitmap image, byte transparency, int x, int y)
        {
            using (Graphics gxSrc = Graphics.FromImage(image))
            {
                var dcDest = gx.GetHdc();
                var dcSource = gxSrc.GetHdc();
                var blendFunct = new BlendFunction()
                {
                    BlendOp = (byte)BlendOperation.AC_SRC_OVER,
                    BlendFlags = (byte)BlendFlags.Zero,
                    SourceConstantAlpha = transparency,
                    AlphaFormat = (byte)0
                };

                PlatformAPIs.AlphaBlend(dcDest, x, y, image.Width, image.Height, dcSource, 0, 0, image.Width, image.Height, blendFunct);
                gx.ReleaseHdc(dcDest);
                gxSrc.ReleaseHdc(dcSource);
            }
        }

        public static void DrawImageAlphaChannel(this Graphics gx, IImage image, int x, int y)
        {
            var imageInfo = new ImageInfo();
            image.GetImageInfo(out imageInfo);
            var rc = new System.Drawing.Rectangle(x, y, (int)imageInfo.Width + x, (int)imageInfo.Height + y);
            IntPtr hdc = gx.GetHdc();
            image.Draw(hdc, ref rc, IntPtr.Zero);
            gx.ReleaseHdc(hdc);
        }

        public static void DrawImageAlphaChannel(this Graphics gx, IImage image, WiMo.Games.Rectangle dest)
        {
            var rc = new System.Drawing.Rectangle(dest.X, dest.Y, dest.Width + dest.X, dest.Height + dest.Y);
            IntPtr hdc = gx.GetHdc();
            image.Draw(hdc, ref rc, IntPtr.Zero);
            gx.ReleaseHdc(hdc);
        }

        public static void DrawImageAlphaChannel(this Graphics graphics, IImage image, WiMo.Games.Rectangle dest, WiMo.Games.Rectangle src)
        {
            var destRect = new System.Drawing.Rectangle(dest.X, dest.Y, dest.Width + dest.X, dest.Height + dest.Y);
            var srcRect = new System.Drawing.Rectangle(src.X, src.Y, src.Width + src.X, src.Height + src.Y);
            IntPtr hdc = graphics.GetHdc();
            image.Draw(hdc, ref destRect, IntPtr.Zero);
            graphics.ReleaseHdc(hdc);
        }

        public static void DrawImage(this Graphics gx, Image image, Location point, Color colorKey)
        {
            using (var attr = new ImageAttributes())
            {
                attr.SetColorKey(colorKey.ToDrawingColor(), colorKey.ToDrawingColor());
                gx.DrawImage(image, new System.Drawing.Rectangle(point.X, point.Y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);
            }
        }

        public static void DrawImage(this Graphics gx, Image image, WiMo.Games.Rectangle destRect)
        {
            using (var imageAttr = new ImageAttributes())
            {
                var colorKey = GetTransparentColor(image);
                imageAttr.SetColorKey(colorKey, colorKey);
                gx.DrawImage(image, destRect.ToDrawingRect(), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
            }
        }
    }
}
